package org.squabble.service;

import java.util.Locale;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import org.squabble.domain.SystemProperty;
import org.squabble.exception.SquabbleException;

@Service
public class EmailService {

	@Autowired
	SystemService systemService;

	@Autowired
	MessageSource messageSource;

	private JavaMailSenderImpl mailSender;

	private void initializeMailSender() throws SquabbleException {
		JavaMailSenderImpl jmsi = new JavaMailSenderImpl();
		SystemProperty serverProperty = systemService
				.getSystemProperty("emailSMTPServer");
		SystemProperty portProperty = systemService
				.getSystemProperty("emailSMTPPort");
		SystemProperty sslProperty = systemService
				.getSystemProperty("emailUseSSL");
		SystemProperty tlsProperty = systemService
				.getSystemProperty("emailUseTLS");
		SystemProperty userProperty = systemService
				.getSystemProperty("emailUser");
		SystemProperty passProperty = systemService
				.getSystemProperty("emailPassword");
		if (serverProperty == null) {
			throw new SquabbleException("error.general.smtpserver");
		}
		if (((Boolean) sslProperty.getValue()).booleanValue()) {
			jmsi.setProtocol("smtps");
		}
		jmsi.setHost(serverProperty.getValue().toString());
		jmsi.setPort(((Long) portProperty.getValue()).intValue());
		jmsi.setUsername(userProperty.getValue().toString());
		jmsi.setPassword(passProperty.getValue().toString());
		Properties p = new Properties();
		if (((Boolean) tlsProperty.getValue()).booleanValue()) {
			p.put("mail.smtp.starttls.enable", true);
		}

		this.mailSender = jmsi;
	}

	private void sendMail(SimpleMailMessage message) throws Exception {
		if (mailSender == null) {
			initializeMailSender();
		}
		if (message.getFrom() == null) {
			SystemProperty addressProperty = systemService
					.getSystemProperty("emailAdress");
			message.setFrom(addressProperty.getValue().toString());
		}
		mailSender.send(message);
	}

	public void sendValidationCode(String email, String validationCode,
			String ipAddress) throws Exception {
		Locale locale = LocaleContextHolder.getLocale();
		SystemProperty siteName = systemService
				.getSystemProperty("systemSiteName");
		SystemProperty url = systemService.getSystemProperty("systemSiteUrl");
		String subject = messageSource.getMessage("email.validation.subject",
				new String[] { siteName.getValue().toString() },
				"email.validation.subject", locale);
		String[] args = new String[] { ipAddress, url.getValue().toString(),
				validationCode, siteName.getValue().toString() };
		String body = messageSource.getMessage("email.validation.body", args,
				validationCode, locale);
		SimpleMailMessage message = new SimpleMailMessage();
		message.setTo(email);
		message.setSubject(subject);
		message.setText(body);
		sendMail(message);
	}

	public void sendNewPassword(String email, String password, String ipAddress)
			throws Exception {
		Locale locale = LocaleContextHolder.getLocale();
		SystemProperty siteName = systemService
				.getSystemProperty("systemSiteName");
		SystemProperty url = systemService.getSystemProperty("systemSiteURL");
		String subject = messageSource.getMessage("email.password.subject",
				new String[] { siteName.getValue().toString() }, "email.password.subject",
				locale);
		String[] args = new String[] { ipAddress, password, url.getValue().toString(),
				siteName.getValue().toString() };
		String body = messageSource.getMessage("email.password.body", args,
				password, locale);
		SimpleMailMessage message = new SimpleMailMessage();
		message.setTo(email);
		message.setSubject(subject);
		message.setText(body);
		sendMail(message);

	}

}
